#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import sys


USAGE = "Usage: python3 %s file" % (__file__)


def help():
    print(USAGE)


def calc_log(filename):
    logfd = open(filename, "r")

    port = 0
    cnt = 0
    total = 0
    calc_flag = 0

    for line in logfd.readlines():
        if line.find("port") > 0 and line.find("data") > 0:
            # print(line)
            part = line.split()
            # print("port %d" % int(part[5], 10))
            if port != int(part[5], 10):
                port = int(part[5], 10)

                calc_flag = True
                cnt = 0
                start_time = part[:2]

            elif calc_flag == True:
                if line.find("{\"gpio\": 27, \"level\": 0}"):
                    cnt += 1
                elif line.find("{\"gpio\": 22, \"level\": 0}"):
                    cnt += 1
                elif line.find("{\"gpio\": 27, \"level\": 1}"):
                    cnt += 1
                elif line.find("{\"gpio\": 22, \"level\": 1}"):
                    cnt += 1

                if cnt >= 4:
                    print(start_time)
                    calc_flag = False
                    total += 1
                    
    logfd.close()

    print("Total play %d, 70/p x 4 = %d" % (total, total * 70 * 4))


def main():

    if len(sys.argv) < 2:
        help()
        return

    print("Start analyze " + sys.argv[1]);
    calc_log(sys.argv[1])


if __name__ == '__main__':
    main()